import {
	createRouter,
	createWebHashHistory,
	createWebHistory
} from "vue-router";
import {
	ElMessage
} from "element-plus";
import {
	useUserStore
} from "@/stores/userStores";



// 管理员模块
const adminRouters = [

	{
		path: "/admin",
		name: "adminIndex",
		component: () => import("@/views/admin/layout/index.vue"),

		children: [{
				path: "",
				name: "adminIndexProject",
				component: () => import("@/views/admin/index/index.vue"),
			},
			{
				path: "/admin/depart",
				name: "adminDepart",
				component: () => import("@/views/admin/depart/depart.vue"),
			},

			{
				path: "/admin/user",
				name: "adminuser",
				component: () => import("@/views/admin/user/user.vue"),
			},

			{
				path: "/admin/project",
				name: "adminproject",
				component: () => import("@/views/admin/project/project.vue"),
			},

			{
				path: "/admin/conFiles",
				name: "adminpconFiles",
				component: () => import("@/views/admin/conFiles/conFiles.vue"),
			},

			{
				path: "/admin/nconType",
				name: "adminnconType",
				component: () => import("@/views/admin/nconType/nconType.vue"),
			},
			{
				path: "/admin/budget",
				name: "adminBudget",
				component: () => import("@/views/admin/budget/budget.vue"),
			},
			{
				path: "/admin/review",
				name: "adminReview",
				component: () => import("@/views/admin/review/review.vue"),
			},
			{
				path: "/admin/nconMaterial",
				name: "adminpnconMaterial",
				component: () => import("@/views/admin/nconMaterial/nconMaterial.vue"),
			},
			{
				path: "/admin/progress",
				name: "adminProgress",
				component: () => import("@/views/admin/progress/progress.vue"),
			},


			{
				path: "/admin/userSelf",
				name: "adminUserSelf",
				component: () => import("@/views/admin/selfUser/user.vue"),
			},

			{
				path: "/admin/projectDetail/:id",
				name: "adminprojectDetail",
				component: () => import("@/views/admin/detail/projectDetail.vue"),
			},

			{
				path: "/admin/fileDetail/:id",
				name: "fileDetail",
				component: () => import("@/views/admin/detail/fileDetail.vue"),
			},
			{
				path: "/admin/noFileDetail/:id",
				name: "noFileDetail",
				component: () => import("@/views/admin/detail/noFileDetail.vue"),
			},



		]
	},
];


// 资料员模块
const clerkRouters = [

	{
		path: "/clerk",
		name: "clerkIndex",
		component: () => import("@/views/clerk/layout/index.vue"),

		children: [{
				path: "",
				name: "indexProject",
				component: () => import("@/views/clerk/index/index.vue"),
			},
			{
				path: "/clerk/depart",
				name: "clerkDepart",
				component: () => import("@/views/clerk/depart/depart.vue"),
			},

			{
				path: "/clerk/user",
				name: "clerkuser",
				component: () => import("@/views/clerk/user/user.vue"),
			},

			{
				path: "/clerk/project",
				name: "clerkproject",
				component: () => import("@/views/clerk/project/project.vue"),
			},

			{
				path: "/clerk/conFiles",
				name: "clerkpconFiles",
				component: () => import("@/views/clerk/conFiles/conFiles.vue"),
			},

			{
				path: "/clerk/nconType",
				name: "clerknconType",
				component: () => import("@/views/clerk/nconType/nconType.vue"),
			},

			{
				path: "/clerk/nconMaterial",
				name: "clerkpnconMaterial",
				component: () => import("@/views/clerk/nconMaterial/nconMaterial.vue"),
			},
			{
				path: "/clerk/progress",
				name: "clerkProgress",
				component: () => import("@/views/clerk/progress/progress.vue"),
			},


			{
				path: "/clerk/userSelf",
				name: "clerkUserSelf",
				component: () => import("@/views/clerk/selfUser/user.vue"),
			},
			{
				path: "/clerk/budget",
				name: "clerkBudget",
				component: () => import("@/views/clerk/budget/budget.vue"),
			},
			{
				path: "/clerk/review",
				name: "clerkReview",
				component: () => import("@/views/clerk/review/review.vue"),
			},
			{
				path: "/clerk/projectDetail/:id",
				name: "clerkProjectDetail",
				component: () => import("@/views/clerk/detail/projectDetail.vue"),
			},
			{
				path: "/clerk/fileDetail/:id",
				name: "clerkFileDetail",
				component: () => import("@/views/clerk/detail/fileDetail.vue"),
			},
			{
				path: "/clerk/noFileDetail/:id",
				name: "clerkNoFileDetail",
				component: () => import("@/views/clerk/detail/noFileDetail.vue"),
			},
			


		]
	},
];








const commonRouters = [

	{
		path: "/login",
		component: () => import("@/views/login.vue"),
	},
	{
		path: "/register",
		component: () => import("@/views/register.vue"),
	}
];

const router = createRouter({
	history: createWebHashHistory(
		import.meta.env.BASE_URL),
	routes: [
		...adminRouters,
		...clerkRouters,


		...commonRouters,
	],
});

const whitelisting = ["/login", "/register"];
// 路由守卫
router.beforeEach((to, from, next) => {
	const userStore = useUserStore();
	// if (userStore.getToken() && to.path.includes("/admin")) {
	// 	const user = userStore.getUserInfo();
	//   console.log("登入的role",user.role)
	// 	if (user.role == "ADMIN") {
	//     next({
	// 			path: "/admin"
	// 		});

	// 	} else if(user.role == "TEACHER"){

	// 		next({
	// 			path: "/teacher"
	// 		});
	// 	} else{
	//     next({
	// 			path: "/doctor"
	// 		});
	//   }
	// }
	if (!whitelisting.includes(to.path) && !userStore.getToken()) {
		next({
			path: "/login"
		});
	} else {
		next();
	}
});

export default router;
